<?php


namespace core;

use Swoole\Coroutine;

class SwooleContext
{

    // 所有协程的变量存储
    protected static $pool = [];

    static function get($key)
    {
        $cid = Coroutine::getuid();
        // 获取当前协程id
        if ($cid < 0)
        {
            return null;
        }
        if(isset(self::$pool[$cid][$key])){
            return self::$pool[$cid][$key];
        }
        return null;
    }

    static function put($key, $item)
    {
        $cid = Coroutine::getuid();
        if ($cid > 0)
        {
            self::$pool[$cid][$key] = $item;
        }

    }

    // 删除当前协程的变量
    static function delete($key = null)
    {
        $cid = Coroutine::getuid();
        if ($cid > 0)
        {
            if($key){
                unset(self::$pool[$cid][$key]);
            }else{
                unset(self::$pool[$cid]);
            }
        }
    }

}
